import example.ConfigurationManager;
import example.DatabaseConnectionPool;

public class SingletonPatternDemo {
    
    public static void main(String[] args) {
        // 测试配置管理器
        testConfigurationManager();
        
        // 测试数据库连接池
        testDatabaseConnectionPool();
    }
    
    private static void testConfigurationManager() {
        System.out.println("=== 测试配置管理器单例 ===");
        
        ConfigurationManager config1 = ConfigurationManager.getInstance();
        ConfigurationManager config2 = ConfigurationManager.getInstance();
        
        // 验证单例
        System.out.println("是否是同一个实例: " + (config1 == config2));
        
        // 使用配置
        String serverUrl = config1.getProperty("server.url", "http://localhost:8080");
        int maxConnections = config1.getIntProperty("max.connections", 100);
        
        System.out.println("服务器地址: " + serverUrl);
        System.out.println("最大连接数: " + maxConnections);
    }
    
    private static void testDatabaseConnectionPool() {
        System.out.println("\n=== 测试数据库连接池单例 ===");
        
        Thread thread1 = new Thread(() -> {
            DatabaseConnectionPool pool = DatabaseConnectionPool.getInstance();
            System.out.println("线程1获取连接池实例: " + pool.hashCode());
        });
        
        Thread thread2 = new Thread(() -> {
            DatabaseConnectionPool pool = DatabaseConnectionPool.getInstance();
            System.out.println("线程2获取连接池实例: " + pool.hashCode());
        });
        
        thread1.start();
        thread2.start();
        
        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}